//
//  TouzhuCell.m
//  SSC
//
//  Created by 红沙尘 on 17/6/17.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "TouzhuCell.h"

@implementation TouzhuCell
{
    __weak IBOutlet UIView *btnView;
    __weak IBOutlet UIView *haomaView;
    __weak IBOutlet NSLayoutConstraint *spaceWidth;
    
    __weak IBOutlet UILabel *weiLabel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    spaceWidth.constant = (ScreenWidth - 12*2 - 40*5)/6;
    
    haomaView.layer.borderWidth   = LineHeight;
    haomaView.layer.cornerRadius  = 2;
    haomaView.layer.masksToBounds = YES;
    haomaView.layer.borderColor   = COLOR_CCCCCC.CGColor;
    
    for (UIButton *btn in btnView.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            
            [btn setTitleColor:Color333333 forState:UIControlStateNormal];
            
            [btn addTarget:self
                    action:@selector(typeChanged:)
          forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    for (UIButton *btn in haomaView.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            btn.layer.cornerRadius = 20;
            btn.layer.masksToBounds = YES;

            [btn addTarget:self
                    action:@selector(qiuPressed:)
          forControlEvents:UIControlEventTouchUpInside];
            
            [self button:btn selected:NO];
        }
    }
}

- (void)loadWithModel:(id)model {
    self.model = model;
    
    weiLabel.text = self.model.wei;
    
    for (UIButton *btn in haomaView.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            HaomaModel *haoma = [self.model haomaAtIndex:btn.tag];
            [self button:btn selected:haoma.selected];
        }
    }
    
    for (UIButton *btn in btnView.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            
            if (btn.tag == self.model.type) {
                [btn setTitleColor:COLOR_FF5253 forState:UIControlStateNormal];
            } else {
                [btn setTitleColor:Color333333 forState:UIControlStateNormal];
            }
        }
    }
}

#pragma mark - private

#pragma mark - 手选号码

- (void)qiuPressed:(UIButton *)button {
    HaomaModel *haoma = [self.model haomaAtIndex:button.tag];
    haoma.selected = !haoma.selected;
    
    [self button:button selected:haoma.selected];
    
    if ([self.tDelegate respondsToSelector:@selector(touzhuCellDidSelected:)]) {
        [self.tDelegate touzhuCellDidSelected:self];
    }
}

#pragma mark 快捷选号

- (void)typeChanged:(UIButton *)button {
    
    [self selectButton:button];
    
    [self.model changedType:button.tag];
    
    for (UIButton *btn in haomaView.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            
            HaomaModel *haoma = [self.model haomaAtIndex:btn.tag];
            haoma.selected = [self stateForHaoma:haoma.number withType:self.model.type];
            [self button:btn selected:haoma.selected];
        }
    }
    
    if ([self.tDelegate respondsToSelector:@selector(touzhuCellDidSelected:)]) {
        [self.tDelegate touzhuCellDidSelected:self];
    }
}

#pragma mark - setupUI

- (void)selectButton:(UIButton *)button {
    
    for (UIButton *btn in btnView.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            
            [btn setTitleColor:Color333333
                      forState:UIControlStateNormal];
        }
    }
    
    [button setTitleColor:COLOR_FF5253 forState:UIControlStateNormal];
}

- (BOOL)stateForHaoma:(int)number withType:(SelectedType)type {
    
    switch (type) {
        case SelectedTypeQUAN:return YES; break;

        case SelectedTypeDA:if (number > 4) return YES; break;
            
        case SelectedTypeXIAO:if (number < 5) return YES; break;
        
        case SelectedTypeJI:if (number%2 == 1) return YES; break;

        case SelectedTypeOU:if (number%2 == 0) return YES; break;

        case SelectedTypeQING:return NO; break;
  
        default:
            break;
    }
    
    return NO;
}

- (void)button:(UIButton *)button selected:(BOOL)selected {
    
    if (selected) {
        button.backgroundColor = COLOR_FF5253;
        button.layer.borderColor = [UIColor clearColor].CGColor;
        button.layer.borderWidth = 0;
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        button.backgroundColor = [UIColor whiteColor];
        button.layer.borderColor = COLOR_999999.CGColor;
        button.layer.borderWidth = LineHeight;
        [button setTitleColor:Color666666 forState:UIControlStateNormal];
    }
}


@end














